{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1eaa963c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from scipy import stats\n",
    "\n",
    "import dowhy\n",
    "from dowhy import CausalModel\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "plt.style.use('fivethirtyeight')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "fc74dab6",
   "metadata": {},
   "outputs": [],
   "source": [
    "COLORS = [\n",
    "    '#00B0F0',\n",
    "    '#FF0000'\n",
    "]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "df565036",
   "metadata": {},
   "source": [
    "# Additional computations"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e42ea37a",
   "metadata": {},
   "source": [
    "## Chapter 01 / Chapter 09"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d28e80e8",
   "metadata": {},
   "source": [
    "### Solving Simpson's paradox with IPW"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "865c3739",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "propensity_score_weighting\n",
      "0.04811580602068774\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\aleks\\anaconda3\\envs\\causal_book_py38\\lib\\site-packages\\sklearn\\utils\\validation.py:1111: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().\n",
      "  y = column_or_1d(y, warn=True)\n"
     ]
    }
   ],
   "source": [
    "pd.read_csv('data/ch_01_drug_data.csv')\n",
    "\n",
    "gender = [1] * (24 + 56 + 17 + 25) + [0] * (3 + 39 + 6 +74)\n",
    "clot = [1] * 24 + [0] * 56 + [1] * 17 + [0] * 25 + [1] * 3 + [0] * 39 + [1] * 6 + [0] * 74\n",
    "drug = [0] * (24 + 56) + [1] * (17 + 25) + [0] * 42 + [1] * 80\n",
    "\n",
    "drug_data = pd.DataFrame(dict(\n",
    "    gender=gender,\n",
    "    clot=clot,\n",
    "    drug=drug\n",
    "))\n",
    "\n",
    "# Construct the graph (the graph is constant for all iterations)\n",
    "nodes_drug = ['drug', 'clot', 'gender']\n",
    "edges_drug = [\n",
    "    ('drug', 'clot'),\n",
    "    ('gender', 'drug'),\n",
    "    ('gender', 'clot')\n",
    "]\n",
    "\n",
    "# Generate the GML graph\n",
    "gml_string_drug = 'graph [directed 1\\n'\n",
    "\n",
    "for node in nodes_drug:\n",
    "    gml_string_drug += f'\\tnode [id \"{node}\" label \"{node}\"]\\n'\n",
    "\n",
    "for edge in edges_drug:\n",
    "    gml_string_drug += f'\\tedge [source \"{edge[0]}\" target \"{edge[1]}\"]\\n'\n",
    "    \n",
    "gml_string_drug += ']'\n",
    "\n",
    "# Instantiate the CausalModel\n",
    "model_drug = CausalModel(\n",
    "    data=drug_data,\n",
    "    treatment='drug',\n",
    "    outcome='clot',\n",
    "    graph=gml_string_drug\n",
    ")\n",
    "\n",
    "# Identify effect\n",
    "estimand_drug = model_drug.identify_effect()\n",
    "\n",
    "# Get estimate (IPW weighting)\n",
    "estimate_drug = model_drug.estimate_effect(\n",
    "    identified_estimand=estimand_drug,\n",
    "    method_name='backdoor.propensity_score_weighting',\n",
    "    target_units='ate'\n",
    ")\n",
    "\n",
    "print(estimate_drug.value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f85982a5",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:causal_book_py38]",
   "language": "python",
   "name": "conda-env-causal_book_py38-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
